﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmCaiDatCoBan : DevExpress.XtraEditors.XtraForm
    {
        private frmMain frmM;
        public frmCaiDatCoBan(frmMain frm)
        {
            InitializeComponent();
            frmM = frm;
            loadKQHT();
            loadXLDV();
            loadHD();
            loadXLCD();
        }

        private void loadKQHT()
        {
            tblThamso_kqht kqht = new tblThamso_kqht();
            DataTable dt = new DataTable();
            dt = kqht.Select();
            if (dt.Rows.Count > 0)
            {
                this.textEditKQHT1.Text = dt.Rows[0]["tu"].ToString();
                this.textEditKQHT2.Text = dt.Rows[1]["tu"].ToString();
                this.textEditKQHT3.Text = dt.Rows[2]["tu"].ToString();
                this.textEditKQHT4.Text = dt.Rows[3]["tu"].ToString();

                this.textEditKQHTTL1.Text = dt.Rows[0]["diem"].ToString();
                this.textEditKQHTTL2.Text = dt.Rows[1]["diem"].ToString();
                this.textEditKQHTTL3.Text = dt.Rows[2]["diem"].ToString();
                this.textEditKQHTTL4.Text = dt.Rows[3]["diem"].ToString();

            }
        }
        private void loadXLDV()
        {
            tblThamso_xldv xldv = new tblThamso_xldv();
            DataTable dt = new DataTable();
            dt = xldv.Select();
            if (dt.Rows.Count > 0)
            {
                this.textEditXLDV1.Text = dt.Rows[0]["tu"].ToString();
                this.textEditXLDV2.Text = dt.Rows[1]["tu"].ToString();
                this.textEditXLDV3.Text = dt.Rows[2]["tu"].ToString();
            }
        }

        private void loadHD()
        {
            tblThamso_hoatdong hd = new tblThamso_hoatdong();
            DataTable dt = new DataTable();
            dt = hd.Select();
            if (dt.Rows.Count > 0)
            {
                this.textEditHDC1.Text = dt.Rows[0]["cong"].ToString();
                this.textEditHDT1.Text = dt.Rows[0]["tru"].ToString();
                this.textEditHDC2.Text = dt.Rows[1]["cong"].ToString();
                this.textEditHDT2.Text = dt.Rows[1]["tru"].ToString();
            }
        }

        private void loadXLCD()
        {
            tblThamso_xlcd cd = new tblThamso_xlcd();
            DataTable dt = new DataTable();
            dt = cd.Select();
            if (dt.Rows.Count > 0)
            {
                this.textEditV1.Text = dt.Rows[0]["tu"].ToString();
                this.textEditV2.Text = dt.Rows[2]["tu"].ToString();
                this.textEditV3.Text = dt.Rows[3]["den"].ToString();
                this.textEditV4.Text = dt.Rows[4]["tu"].ToString();
                this.textEditV5.Text = dt.Rows[5]["tu"].ToString();
                this.textEditV6.Text = dt.Rows[5]["den"].ToString();
                this.textEditV7.Text = dt.Rows[6]["den"].ToString();
                this.textEditV8.Text = dt.Rows[7]["tu"].ToString();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                int tkqht1 = int.Parse(this.textEditKQHT1.Text);
                int tkqht2 = int.Parse(this.textEditKQHT2.Text);
                int tkqht3 = int.Parse(this.textEditKQHT3.Text);
                int tkqht4 = int.Parse(this.textEditKQHT4.Text);

                int dkqht1 = int.Parse(this.textEditKQHTTL1.Text);
                int dkqht2 = int.Parse(this.textEditKQHTTL2.Text);
                int dkqht3 = int.Parse(this.textEditKQHTTL3.Text);
                int dkqht4 = int.Parse(this.textEditKQHTTL4.Text);
                if (tkqht2 >= tkqht1 || tkqht3 >= tkqht2)
                {
                    MessageBox.Show("Điểm kết quả học tập được nhập từ cao xuống thấp!!!", "Thông báo");
                    return;
                }
                tblThamso_kqht kqht = new tblThamso_kqht();
                kqht.thamso_kqht(tkqht1, tkqht2, tkqht3, tkqht4, dkqht1, dkqht2, dkqht3, dkqht4);

                int txldv1 = int.Parse(this.textEditXLDV1.Text);
                int txldv2 = int.Parse(this.textEditXLDV2.Text);
                int txldv3 = int.Parse(this.textEditXLDV3.Text);
                tblThamso_xldv xldv = new tblThamso_xldv();
                xldv.thamso_xldv(txldv1, txldv2, txldv3);


                int hdc1 = int.Parse(this.textEditHDC1.Text);
                int hdt1 = int.Parse(this.textEditHDT1.Text);
                int hdc2 = int.Parse(this.textEditHDC2.Text);
                int hdt2 = int.Parse(this.textEditHDT2.Text);
                tblThamso_hoatdong hd = new tblThamso_hoatdong();
                hd.thamso_hoatdong(hdc1, hdt1, hdc2, hdt2);

                int v1 = int.Parse(this.textEditV1.Text);
                int v2 = int.Parse(this.textEditV2.Text);
                int v3 = int.Parse(this.textEditV3.Text);
                int v4 = int.Parse(this.textEditV4.Text);
                int v5 = int.Parse(this.textEditV5.Text);
                int v6 = int.Parse(this.textEditV6.Text);
                int v7 = int.Parse(this.textEditV7.Text);
                int v8 = int.Parse(this.textEditV8.Text);
                tblThamso_xlcd xlcd = new tblThamso_xlcd();
                xlcd.thamso_xlcd(v1, v2, v3, v4, v5, v6, v7, v8);

                MessageBox.Show("Cài đặt thành công !", "Thông báo");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void frmCaiDatCoBan_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmM.frmCDCB_isShown = false;
        }
    }
}